from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from client_model import *


class StyleSheet(object):
    DefaultListView = "QListView{border-style:none;font-family:微软雅黑;}QWidget::item { border-style:none;padding:5px;}QWidget::item:hover{background:#e0f3fe}QWidget::item:selected{background:#bce4fc;color:black}"
    DefaultButton = "QWidget { border-style:none;background:#26a6f2;padding:2px 0px 2px 0px;color:white;font-family:微软雅黑;}QWidget:hover {background:#3688ff}QWidget:pressed {background:#007acc}"
    BigButton = "QWidget { font-size:20px;font-weight:700}"
    DefaultLabel = "QLabel{font-family:微软雅黑;}"
    InfoLabel = "QLabel{padding:5px;background:#f0faff}"
    DefaultLineEdit = "QLabel{font-family:微软雅黑;}"
    DefaultToolBar = "QToolBar{background:#e0f3fe;padding:0}QToolButton{border-style:none;}QToolButton:hover{background:#bce4fc}"
    SendToolBar = "QToolBar{background:#bce4fc}QToolButton{padding:4px;margin:0px 3px}QToolButton:hover{background:#e0f3fe}QToolButton:pressed{background:#f0faff}"
    DefaultTextEidt = "QTextEdit{border-style:none;font-family:微软雅黑;background:#f0faff}"
    SendTextEdit = "QTextEdit{background:#ffffff}"
    DefaultTextBrowser = "QTextBrowser{border-style:none;font-family:微软雅黑;background:#f0faff;color:#1e1e1e;}"
    CtrlBtn = "QPushButton{border-style:none;background:#bce4fc;font-family:微软雅黑;}QPushButton:hover{background:#80CBF9}"
    ChatListView = "QListView{background:#bce4fc}QWidget::item:selected{background:#f0faff;color:black}QWidget::item:hover{background:#e0f3fe}"


class ChatSideBar(QWidget):

    def __init__(self, parent=None):
        super(ChatSideBar, self).__init__(parent)
        self.initUI()

    def initUI(self):
        self.chatlistview = QListView()
        self.chatlistview.setStyleSheet(
            StyleSheet.DefaultListView + StyleSheet.ChatListView)
        self.chatlistview.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.chatlistview.setFixedWidth(150)
        self.chatlistview.setContextMenuPolicy(Qt.CustomContextMenu)
        self.chatlistview.setSelectionRectVisible(False)
        vbox = QVBoxLayout()
        vbox.setContentsMargins(0, 0, 0, 0)
        vbox.setSpacing(0)
        vbox.addWidget(self.chatlistview)
        self.setLayout(vbox)
        self.setFixedWidth(150)


class ChatBox(QWidget):

    def __init__(self, parent=None):
        super(ChatBox, self).__init__(parent)
        self.initUI()

    def initUI(self):
        vbox = QVBoxLayout()
        vbox.setContentsMargins(0, 0, 0, 5)
        vbox.setSpacing(0)
        self.videoLabel=QLabel()
        self.videoLabel.setSizePolicy(
            QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.videoLabel.setHidden(True)
        self.receiveBox = QTextBrowser()
        # self.receiveBox.setEnabled(False)
        self.receiveBox.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.receiveBox.setStyleSheet(StyleSheet.DefaultTextBrowser)
        toolbar = QToolBar()
        toolbar.setStyleSheet(
            StyleSheet.DefaultToolBar + StyleSheet.SendToolBar)
        self.audioAction = QAction(QIcon('./ico/audio.ico'), 'Audio', self)
        self.videoAction = QAction(QIcon('./ico/video.ico'), 'Video', self)
        self.fileAction = QAction(QIcon('./ico/file.ico'), 'File', self)
        self.sendAction = QAction(QIcon('./ico/send.ico'), 'Send', self)
        spaceWidgets = QWidget()
        spaceWidgets.setSizePolicy(
            QSizePolicy.Expanding, QSizePolicy.Expanding)
        toolbar.addAction(self.audioAction)
        toolbar.addAction(self.videoAction)
        toolbar.addAction(self.fileAction)
        toolbar.addWidget(spaceWidgets)
        toolbar.addAction(self.sendAction)

        self.sendBox = QTextEdit()
        self.sendBox.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.sendBox.setStyleSheet(
            StyleSheet.DefaultTextEidt + StyleSheet.SendTextEdit)
        self.sendBox.setFixedHeight(70)
        self.sendBox.setSizePolicy(
            QSizePolicy.Expanding, QSizePolicy.Expanding)
        hbox = QHBoxLayout()
        hbox.addStretch()
        vbox.addWidget(self.videoLabel)
        vbox.addWidget(self.receiveBox)
        vbox.addWidget(toolbar)
        vbox.addWidget(self.sendBox)
        vbox.addLayout(hbox)
        self.setLayout(vbox)
        palette1 = QPalette()
        palette1.setColor(self.backgroundRole(), QColor(188, 228, 252))
        self.setPalette(palette1)
        self.setAutoFillBackground(True)
        self.setHidden(True)


class MainUi(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        """
            initialize mainwindow ui
        """
        vbox = QVBoxLayout()
        # vbox.setContentsMargins(1, 1, 1, 1)
        vbox.setContentsMargins(0, 0, 0, 0)
        vbox.setSpacing(0)
        title = Title()

        # set window style and color
        # self.setWindowFlags(Qt.SplashScreen)
        # self.setWindowFlags(Qt.FramelessWindowHint)
        palette1 = QPalette()
        palette1.setColor(self.backgroundRole(), QColor(200, 200, 200))
        self.setPalette(palette1)
        self.setAutoFillBackground(True)

        # init horizontal layout
        hbox = QHBoxLayout()
        hbox.setContentsMargins(0, 0, 0, 0)
        hbox.setSpacing(0)
        # init widgets
        self.chatSideBar = ChatSideBar()
        self.emptyMain = EmptyMain()
        self.chatBox = ChatBox()
        self.rightSideCtrlBtn = QPushButton()
        self.rightSideCtrlBtn.setStyleSheet(StyleSheet.CtrlBtn)
        self.rightSideCtrlBtn.setSizePolicy(
            QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.rightSideCtrlBtn.setFixedWidth(10)
        self.rightSideCtrlBtn.setEnabled(False)
        self.contactsSidebar = ContactsSidebar()
        self.connectSidebar = ConnectSidebar()
        self.loginSidebar = LoginSidebar(self)
        self.registerSidebar = RegisterSidebar()

        # horizontal layout
        hbox.addWidget(self.chatSideBar)
        hbox.addWidget(self.emptyMain)
        hbox.addWidget(self.chatBox)
        hbox.addWidget(self.rightSideCtrlBtn)
        hbox.addWidget(self.contactsSidebar)
        hbox.addWidget(self.connectSidebar)
        hbox.addWidget(self.loginSidebar)
        hbox.addWidget(self.registerSidebar)
        vbox.addWidget(title)
        vbox.addLayout(hbox)
        self.setLayout(vbox)
        self.setWindowIcon(QIcon('./ico/logo.ico'))
        self.setWindowFlag(Qt.Window)
        self.setMinimumSize(500, 300)
        self.resize(700, 400)
        self.center()
        self.setWindowTitle('多媒体终端控制台')
    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
            event.accept()

    def mouseMoveEvent(self, event):
        try:
            if event.buttons() == Qt.LeftButton:
                self.move(event.globalPos() - self.dragPosition)
                event.accept()
        except:
            pass


class Title(QWidget):

    def __init__(self, parent=None):
        super(Title, self).__init__(parent)
        self.initUI()

    def initUI(self):
        height = 10
        self.setFixedHeight(height)
        palette = QPalette()
        # palette.setColor(self.backgroundRole(), QColor(127, 207, 255))
        palette.setColor(self.backgroundRole(), QColor(188, 228, 252))
        self.setPalette(palette)
        self.setAutoFillBackground(True)


class EmptyMain(QWidget):

    def __init__(self, parent=None):
        super(EmptyMain, self).__init__(parent)
        self.initUI()

    def initUI(self):
        palette = QPalette()
        palette.setColor(self.backgroundRole(), QColor(188, 228, 252))
        self.setPalette(palette)
        self.setAutoFillBackground(True)


class ContactsSidebar(QWidget):

    def __init__(self, parent=None):
        super(ContactsSidebar, self).__init__(parent)
        self.initUI()

    def initUI(self):

        vbox = QVBoxLayout()
        vbox.setContentsMargins(0, 0, 0, 0)
        vbox.setSpacing(0)

        self.exitAction = QAction(QIcon('./ico/exit.ico'), 'Exit', self)
        self.exitAction.setShortcut('Ctrl+Q')
        self.exitAction.triggered.connect(qApp.quit)
        self.refreshAction = QAction(
            QIcon('./ico/refresh.ico'), 'Refresh', self)
        self.refreshAction.setShortcut('Ctrl+R')
        self.userlistView = QListView()
        self.userlistView.setStyleSheet(StyleSheet.DefaultListView)
        self.userlistView.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.userlistView.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.userlistView.setFixedWidth(150)
        self.userlistView.setContextMenuPolicy(Qt.CustomContextMenu)
        self.userlistView.customContextMenuRequested.connect(
            self.myListWidgetContext)
        spaceWidgets = QWidget()
        spaceWidgets.setSizePolicy(
            QSizePolicy.Expanding, QSizePolicy.Expanding)
        toolbar = QToolBar()
        toolbar.addWidget(spaceWidgets)
        toolbar.addAction(self.refreshAction)
        toolbar.addAction(self.exitAction)
        toolbar.setStyleSheet(StyleSheet.DefaultToolBar)
        self.infoBar = QLabel()
        self.infoBar.setText("你好！\nboom")
        self.infoBar.setFixedHeight(40)
        self.infoBar.setStyleSheet(
            StyleSheet.DefaultLabel + StyleSheet.InfoLabel)
        vbox.addWidget(self.userlistView)
        vbox.addWidget(self.infoBar)
        vbox.addWidget(toolbar)
        self.setLayout(vbox)
        self.setFixedWidth(150)
        self.setHidden(True)

        self.chatAction = QAction(u'发消息', self)
        self.addfAction = QAction(u'添加好友', self)
        self.delfAction = QAction(u'删除好友', self)

    def setInfo(self, data):
        self.infoBar.setText(data)

    def myListWidgetContext(self, point):
        popMenu = QMenu()
        popMenu.addAction(self.chatAction)
        popMenu.addAction(self.addfAction)
        popMenu.addAction(self.delfAction)
        popMenu.exec_(QCursor.pos())


class RegisterSidebar(QWidget):

    def __init__(self, parent=None):
        super(RegisterSidebar, self).__init__(parent)
        self.initUI()

    def initUI(self):
        vbox = QVBoxLayout()
        idLabel = QLabel('用户名')
        idLabel.setStyleSheet(StyleSheet.DefaultLabel)
        self.idLineEdit = QLineEdit()
        self.idLineEdit.setStyleSheet(StyleSheet.DefaultLineEdit)
        pwdLabel = QLabel('密码')
        pwdLabel.setStyleSheet(StyleSheet.DefaultLabel)
        self.pwdLineEdit = QLineEdit()
        self.pwdLineEdit.setStyleSheet(StyleSheet.DefaultLineEdit)
        self.pwdLineEdit.setEchoMode(QLineEdit.Password)
        grid = QGridLayout()
        grid.addWidget(idLabel, 1, 1)
        grid.addWidget(self.idLineEdit, 1, 2)
        grid.addWidget(pwdLabel, 2, 1)
        grid.addWidget(self.pwdLineEdit, 2, 2)
        self.returnButton = QPushButton('返回')
        self.registerButton = QPushButton('注册')
        vbox.addStretch(1)
        vbox.addLayout(grid)
        vbox.addWidget(self.registerButton)
        vbox.addWidget(self.returnButton)
        self.setLayout(vbox)
        self.setFixedWidth(150)
        self.setHidden(True)

    def set_enabled(self, data):
        self.idLineEdit.setEnabled(data)
        self.pwdLineEdit.setEnabled(data)
        self.registerButton.setEnabled(data)
        self.returnButton.setEnabled(data)

    def registing(self):
        self.set_enabled(False)
        self.registerButton.setText("正在注册")

    def register_fail(self):
        self.set_enabled(True)
        self.registerButton.setText("重试")

    def registed(self):
        self.set_enabled(True)
        self.registerButton.setEnabled(False)
        self.registerButton.setText("注册成功")


class LoginSidebar(QWidget):

    def __init__(self, parent=None):
        super(LoginSidebar, self).__init__(parent)
        self.initUI()

    def initUI(self):
        palette1 = QPalette()
        palette1.setColor(self.backgroundRole(), QColor(255, 255, 255))
        self.setPalette(palette1)
        self.setAutoFillBackground(True)
        vbox = QVBoxLayout()
        idLabel = QLabel('ID')
        idLabel.setStyleSheet(StyleSheet.DefaultLabel)
        self.idLineEdit = QLineEdit('126465')
        self.idLineEdit.setStyleSheet(StyleSheet.DefaultLineEdit)
        pwdLabel = QLabel('密码')
        pwdLabel.setStyleSheet(StyleSheet.DefaultLabel)
        self.pwdLineEdit = QLineEdit('123')
        self.pwdLineEdit.setStyleSheet(StyleSheet.DefaultLineEdit)
        self.pwdLineEdit.setEchoMode(QLineEdit.Password)
        grid = QGridLayout()
        grid.addWidget(idLabel, 1, 1)
        grid.addWidget(self.idLineEdit, 1, 2)
        grid.addWidget(pwdLabel, 2, 1)
        grid.addWidget(self.pwdLineEdit, 2, 2)
        self.loginButton = QPushButton('登录')
        self.loginButton.setStyleSheet(StyleSheet.DefaultButton)
        self.registerButton = QPushButton('注册')
        self.registerButton.setStyleSheet(StyleSheet.DefaultButton)
        vbox.addStretch(1)
        vbox.addLayout(grid)
        vbox.addWidget(self.loginButton)
        vbox.addWidget(self.registerButton)
        self.setLayout(vbox)
        self.setFixedWidth(150)
        self.setHidden(True)

    def set_enabled(self, data):
        self.idLineEdit.setEnabled(data)
        self.pwdLineEdit.setEnabled(data)
        self.loginButton.setEnabled(data)
        self.registerButton.setEnabled(data)

    def loggingin(self):
        self.set_enabled(False)
        self.loginButton.setText("正在登录")

    def login_fail(self):
        self.set_enabled(True)
        self.loginButton.setText("重试")

    def loggedin(self):
        self.set_enabled(True)
        self.loginButton.setEnabled(False)
        self.loginButton.setText("登录成功")

    def registed(self, user_id):
        self.registerButton.setEnabled(False)
        self.registerButton.setText("注册成功")
        self.idLineEdit.setText(user_id)
        self.pwdLineEdit.setText("")
        self.loginButton.setFocus()


class ConnectSidebar(QWidget):

    def __init__(self, parent=None):
        super(ConnectSidebar, self).__init__(parent)
        self.initUI()

    def initUI(self):
        palette1 = QPalette()
        palette1.setColor(self.backgroundRole(), QColor(255, 255, 255))
        self.setPalette(palette1)
        self.setAutoFillBackground(True)

        vbox = QVBoxLayout()
        addrLabel = QLabel('地址')
        addrLabel.setStyleSheet(StyleSheet.DefaultLabel)
        # self.addrLineEdit = QLineEdit("127.0.0.1")
        self.addrLineEdit = QLineEdit("10.102.24.227")
        self.addrLineEdit.setStyleSheet(StyleSheet.DefaultLineEdit)
        portLabel = QLabel('端口')
        portLabel.setStyleSheet(StyleSheet.DefaultLabel)
        self.portLineEdit = QLineEdit("1883")
        self.portLineEdit.setStyleSheet(StyleSheet.DefaultLineEdit)
        grid = QGridLayout()
        grid.addWidget(addrLabel, 1, 1)
        grid.addWidget(self.addrLineEdit, 1, 2)
        grid.addWidget(portLabel, 2, 1)
        grid.addWidget(self.portLineEdit, 2, 2)
        self.connectButton = QPushButton('连接')
        self.connectButton.setStyleSheet(
            StyleSheet.DefaultButton + StyleSheet.BigButton)
        vbox.addStretch(1)
        vbox.addLayout(grid)
        vbox.addWidget(self.connectButton)
        self.setLayout(vbox)
        self.setFixedWidth(150)

    def set_enabled(self, data):
        self.addrLineEdit.setEnabled(data)
        self.portLineEdit.setEnabled(data)
        self.connectButton.setEnabled(data)

    def connecting(self):
        self.set_enabled(False)
        self.connectButton.setText("正在连接")

    def connection_fail(self):
        self.set_enabled(True)
        self.connectButton.setText("重试")

    def connected(self):
        self.set_enabled(False)
        self.connectButton.setText("已连接")

    def get_addr(self):
        return self.addrLineEdit.text(), int(self.portLineEdit.text())
